﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Tb.App.API.Applications.Command.UploadImage;
using Tb.App.API.Applications.DTOs;
using Tb.App.ErrorCode;

namespace Tb.App.API.Controllers.Rabbit
{
    /// <summary>
    /// 上传多张图片控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UploadBatchImageController : ControllerBase
    {
        private readonly ILogger<UploadBatchImageController> logger;
        private readonly IMediator mediator;

        public UploadBatchImageController(ILogger<UploadBatchImageController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回id</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UploadBatch([FromForm]UploadBatchImageCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 查询图片列表
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回查询任务</returns>
        [HttpGet]
        public async Task<ApiResult<List<BatchImageDto>>> GetBatchImagesQuery([FromQuery]GetBatchImagesQueryCommand request)
        {
            try
            {
                return await  mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="Id">编号</param>
        /// <returns>返回删除任务</returns>
        [HttpDelete]
        public async Task<ApiResult<int>> DelBatchImage(int Id, [FromQuery] string ImageType)
        {
            try
            {
                return await mediator.Send(new DelBatchImageCommand { Id=Id,ImageType=ImageType});
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);
                throw;
            }
        }
    }
}
